﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TVDL4.VO;
using TVDL4.BUS;
using System.IO;

namespace TVDL4
{
    public partial class frmCongTrinh :  frmParent
    {
        private const string XML_FILE_LOAICONGTRINH = "LoaiCongTrinhObject.xml";
        private CongTrinhBUS congtrinhBUS;
        private Module.ClickButtonStatus flagButton = Module.ClickButtonStatus.Ngung;//co phan biet dang o trang thai ghi hay ngung
        public frmCongTrinh()
        {
            InitializeComponent();
            congtrinhBUS = new CongTrinhBUS();
        }


        #region "Private Method"
        private void ClearControl()
        {
            txtCongTrinh.Text = "";
            cboLoaiCongTrinh.Text = "";
        }
        private void UpdateControl()
        {
            flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["TenCongTrinh"] = txtCongTrinh.Text;
            flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["LoaiCongTrinh"] = cboLoaiCongTrinh.Text;
        }
        private void StatusButton()
        {
            if (flagButton == Module.ClickButtonStatus.Them || flagButton == Module.ClickButtonStatus.Sua)
            {
                btnThem.Text = "&Ghi";
                btnSua.Text = "&Ngưng";
                btnXoa.Enabled = false;
                btnThoat.Enabled = false;
                flexCongTrinh.Enabled = false;
                txtCongTrinh.Focus();

            }
            else
            {
                btnThem.Text = "&Thêm";
                btnSua.Text = "&Sửa";
                btnXoa.Enabled = true;
                btnThoat.Enabled = true;
                flexCongTrinh.Enabled = true;

            }
        }

        #endregion

        private void flexCongTrinh_AfterRowColChange(object sender, C1.Win.C1FlexGrid.RangeEventArgs e)
        {
            if (flexCongTrinh.Selection.TopRow >= 1 && flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["TenCongTrinh"] != null)
            {
                txtCongTrinh.Text = flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["TenCongTrinh"].ToString();
                cboLoaiCongTrinh.Text = flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["LoaiCongTrinh"].ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["HOANTHANH"].ToString() == "1") { MessageBox.Show("Công trình đã hoàn thành."); return; }
            if (flexCongTrinh.Selection.TopRow >= 1 && flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["KEYSL"].ToString() != "")
            {
                if (congtrinhBUS.DeleteRecord(flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["KEYSL"].ToString()))
                {
                    flexCongTrinh.Rows.Remove(flexCongTrinh.Selection.TopRow);
                    ClearControl();
                }

            }
        }

        public void btnThem_Click(object sender, EventArgs e)
        {
            CongTrinhVO congtrinhVO;
            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung :
                     flagButton = Module.ClickButtonStatus.Them;
                     ClearControl();
                     break;
                case Module.ClickButtonStatus.Them:
                     congtrinhVO = new CongTrinhVO(null,txtCongTrinh.Text,cboLoaiCongTrinh.Text);
                     congtrinhVO = congtrinhBUS.InsertRecord(congtrinhVO);
                     if (congtrinhVO == null)
                         MessageBox.Show("Không ghi được dữ liệu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     else
                     {
                         flexCongTrinh.Rows.Count++;
                         flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["STT"] = flexCongTrinh.Rows.Count - 1;
                         flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["KEYSL"] = congtrinhVO.KeySL;
                         flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["TenCongTrinh"] = congtrinhVO.TenCongTrinh;
                         flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["LoaiCongTrinh"] = congtrinhVO.LoaiCongTrinh;
                         flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["HOANTHANH"] = congtrinhVO.HoanThanh;
                         ClearControl();
                     }
                     ClearControl();
                     flagButton = Module.ClickButtonStatus.Ngung;
                     break;
                case Module.ClickButtonStatus.Sua:
                    congtrinhVO = new CongTrinhVO(flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["KEYSL"].ToString(), txtCongTrinh.Text, cboLoaiCongTrinh.Text);
                    congtrinhBUS.UpdateRecord(congtrinhVO);
                    UpdateControl();
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;
            }
            StatusButton();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            switch (flagButton)
            {
                case Module.ClickButtonStatus.Ngung:
                    flagButton = Module.ClickButtonStatus.Sua;
                    break;
                case Module.ClickButtonStatus.Sua:
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;
                case Module.ClickButtonStatus.Them:
                    flagButton = Module.ClickButtonStatus.Ngung;
                    break;

            }
            StatusButton();
        }

        private void frmCongTrinh_Load(object sender, EventArgs e)
        {
            flexCongTrinh.Rows.Count = 1;
            List<CongTrinhVO> lstCongTrinh = congtrinhBUS.GetListCongTrinh();
            foreach (CongTrinhVO congtrinhVO in lstCongTrinh)
            {
                flexCongTrinh.Rows.Count++;
                flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["STT"] = flexCongTrinh.Rows.Count - 1;
                flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["KEYSL"] = congtrinhVO.KeySL;
                flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["TenCongTrinh"] = congtrinhVO.TenCongTrinh;
                flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["LoaiCongTrinh"] = congtrinhVO.LoaiCongTrinh;
                flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1]["HOANTHANH"] = congtrinhVO.HoanThanh;
                if (congtrinhVO.HoanThanh == 1)
                {
                    C1.Win.C1FlexGrid.CellStyle rs = this.flexCongTrinh.Styles.Add("rowcolor");
                    rs.ForeColor = Color.Blue;
                    flexCongTrinh.Rows[flexCongTrinh.Rows.Count - 1].Style = rs;
                }

            }
            GetLoaiCongTrinh();
        }
        private void GetLoaiCongTrinh()
        {
            // Load the customer object from the existing XML file (if any)...
            if (File.Exists(XML_FILE_LOAICONGTRINH) == true)
            {
                LoaiCongTrinh loaicongtrinh = ObjectXMLSerializer<LoaiCongTrinh>.Load(XML_FILE_LOAICONGTRINH);
                this.LoadLoaiCongTrinhToCombo(loaicongtrinh);
            }

        }
        /// <summary>
        /// Load Nguyen quan len combo
        /// </summary>
        /// <param name="loaicongtrinh"></param>
        private void LoadLoaiCongTrinhToCombo(LoaiCongTrinh loaicongtrinh)
        {

            cboLoaiCongTrinh.ClearItems();
            cboLoaiCongTrinh.DefColWidth = 100;

            loaicongtrinh.Names.Sort();
            foreach (string Item in loaicongtrinh.Names)
            {
                cboLoaiCongTrinh.AddItem(Item);
            }
            cboLoaiCongTrinh.AllowSort = true;
            cboLoaiCongTrinh.Columns[0].Caption = "Name";
            cboLoaiCongTrinh.ColumnWidth = 250;


        }

        private void btnHoanThanh_Click(object sender, EventArgs e)
        {
            CongTrinhVO congtrinhVO;
            congtrinhVO = new CongTrinhVO(flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["KEYSL"].ToString(), txtCongTrinh.Text, cboLoaiCongTrinh.Text);
            congtrinhBUS.UpdateHoanThanh(congtrinhVO);
            flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow]["HOANTHANH"] = 1;
            C1.Win.C1FlexGrid.CellStyle rs = this.flexCongTrinh.Styles.Add("rowcolor");
            rs.ForeColor = Color.Blue;
            flexCongTrinh.Rows[flexCongTrinh.Selection.TopRow].Style = rs;
        }
        

    }
}
